﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;

using Detai.Site.Models;
using Detai.Site.Components;
using Detai.Lib.Common;
using Detai.Site.ViewModels;

namespace Detai.Site.Controllers
{

    [HandleError]
    public class AccountController : Controller
    {
        DetaiDataContext eDataContext = new DetaiDataContext();

        public ActionResult LogOn()
        {
            return View();
        }

        [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            try
            {
                var currentUser = eDataContext.TaiKhoans.Where(e => e.Tendangnhap == model.UserName && e.Matkhau == model.Password).FirstOrDefault();
                if (currentUser != null)
                {
                    var temp = new BaseAuthentication();
                    temp.Login(currentUser.Tendangnhap);
                    return RedirectToAction("Trangchu", "Home");
                }
                else
                {
                    ViewData[Messages.MessageInfo] = "Thông báo: Tên đăng nhập \"" + model.UserName + "\" hoặc mật khẩu không tồn tại trong hệ thống";

                    return View();
                }
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }

        // **************************************
        // URL: /Account/LogOff
        // **************************************

        public ActionResult LogOff()
        {
         //   FormsService.SignOut();
            var temp = new BaseAuthentication();
            temp.Logout();
            return RedirectToAction("Index", "Home");
        }

        // **************************************
        // URL: /Account/Register
        // **************************************

        public ActionResult Dangky()
        {
            AccountViewModel model = new AccountViewModel();
            model.Lophocs = eDataContext.Lops.ToList();
            return View(model);
        }
        [HttpPost]
        public ActionResult Dangky(AccountViewModel formdata)
        {
            formdata.TaiKhoan.LoaiTK = 3;
            formdata.Sinhvien.Ngaysinh = Request["txtNgaysinh"] != null && Request["txtNgaysinh"].ToString() != string.Empty ? Utils.ParseDateTime(Request["txtNgaysinh"].ToString(), new System.Globalization.CultureInfo("vi-VN")) : (DateTime?)null;
            // Thêm vào bảng tài khoản
            eDataContext.TaiKhoans.InsertOnSubmit(formdata.TaiKhoan);
            eDataContext.SubmitChanges();
            // Thêm vào bảng sinh viên
            formdata.Sinhvien.MaTK = formdata.TaiKhoan.MaTK;
            eDataContext.Sinhviens.InsertOnSubmit(formdata.Sinhvien);
            eDataContext.SubmitChanges();
            var msg = "Bạn đã đăng ký tài khoản thành công!";
            return RedirectToAction("Index", "Thongbao", new { message = msg });
        }

        public ActionResult GiangvienDangky()
        {
            AccountGVDangkyViewModel model = new AccountGVDangkyViewModel();
            return View(model);
        }
        [HttpPost]
        public ActionResult GiangvienDangky(AccountGVDangkyViewModel formdata)
        {
            formdata.TaiKhoan.LoaiTK = 2;
            eDataContext.TaiKhoans.InsertOnSubmit(formdata.TaiKhoan);
            eDataContext.SubmitChanges();
            formdata.Giangvien.MaTK = formdata.TaiKhoan.MaTK;
            eDataContext.Giangviens.InsertOnSubmit(formdata.Giangvien);
            eDataContext.SubmitChanges();
            return RedirectToAction("Index", "Thongbao", new { message="Bạn đã đăng ký tài khoản thành công."});
        }
        // **************************************
        // URL: /Account/ChangePassword
        // **************************************

        [Authorize]
        public ActionResult ChangePassword()
        {
            //ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult ChangePassword(ChangePasswordModel model)
        {
            //if (ModelState.IsValid)
            //{
            //    if (MembershipService.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword))
            //    {
            //        return RedirectToAction("ChangePasswordSuccess");
            //    }
            //    else
            //    {
            //        ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
            //    }
            //}

            //// If we got this far, something failed, redisplay form
            //ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
            return View(model);
        }

        // **************************************
        // URL: /Account/ChangePasswordSuccess
        // **************************************

        public ActionResult ChangePasswordSuccess()
        {
            return View();
        }

    }
}
